﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace Kugar.Core.HeadlessBrowser
{
    public class Window
    {
        public Window(Window parentWindow)
        {
            closed = false;
            defaultStatus = "Kugar.Core Headless Browser";
            opener = parentWindow;
            parent = parentWindow;
        }

        public bool Focused { get; private set; }

        #region "Html DOM 属性"

        public HtmlAgilityPack.HtmlNode[] frames { get; private set; }

        public bool closed { get; internal set; }
        public string defaultStatus { get; set; }
        public Document document { private set; get; }
        public History history { private set; get; }
        public int innerheight { get; internal set; }
        public int innerwidth { get; internal set; }

        public int length
        {
            get { return document.GetFrameCount(); }
        }

        public Location location { get; private set; }
        public string name { set; get; }
        public Navigator Navigator { get; private set; }
        public object opener { get; private set; }

        public int outerheight { get; internal set; }
        public int outerwidth { get; internal set; }

        public int pageXOffset { get; internal set; }
        public int pageYOffset { get; internal set; }

        public Window parent { get; private set; }
        public Screen Screen { get; private set; }

        public Window self { get { return this; } }
        public string status { set; get; }
        public Window top { get; internal set; }

        public Window window { get { return this; } }

        public int screenLeft { get; internal set; }
        public int screenTop { get; internal set; }

        public int screenX { get; internal set; }
        public int screenY { get; internal set; }

        #endregion


        #region "方法"

        /// <summary>
        ///     显示带有一段消息和一个确认按钮的警告框
        /// </summary>
        /// <param name="message"></param>
        public void alert(string message) { }

        /// <summary>
        ///     把键盘焦点从顶层窗口移开
        /// </summary>
        public void blur() { }

        public void clearInterval(int id_of_setinterval)
        {

        }

        public void clearTimeout(int id_of_setinterval)
        {

        }

        public void close() { }

        public void confirm() { }

        public Window createPopup()
        {
            return new Window(this);
        }

        public void focus()
        {

        }

        public void moveBy(int x, int y)
        {
            this.screenX += x;
            this.screenY += y;

            this.screenLeft += x;
            this.screenTop += y;


        }

        public void moveTo(int x, int y)
        {
            this.screenX = x;
            this.screenY = y;

            this.screenLeft = x;
            this.screenTop = y;
        }

        public void open(string URL = "about:blank", string name = "", string features = "", string replace = "")
        {
            //TODO:打开新窗口
        }

        public void print()
        { }

        public void prompt()
        {
            //TODO:引发提示输入框的事件
        }

        public void resizeBy(int width, int height = 0)
        {
            this.innerwidth += width;
            this.innerheight += height;
        }

        public void resizeTo(int width, int? height = null)
        {
            if (height.HasValue)
            {
                this.innerheight = height.Value;
            }

            this.innerwidth = width;
        }

        public void scrollBy() { }

        public void scrollTo() { }

        public int setInterval(string code, int millisec)
        {

        }

        public int setTimeout(string code, int millisec)
        {

        }


        #endregion

        #region "事件"

        public event EventHandler Closing;
        public event EventHandler Closed;

        public event EventHandler Focusing;
        public event EventHandler Focued;

        #endregion
    }

    public class Document
    {
        private HtmlAgilityPack.HtmlDocument document = null;



        public int GetFrameCount()
        {
            return 0;
        }

        public void write(params string[] text)
        {


            var node = new HtmlNode(HtmlNodeType.Text, document, -1);

            string innerhtml = "";

            for (int i = 0; i < text.Length; i++)
            {
                innerhtml += text[i];
            }

            node.InnerHtml = innerhtml;

            document.DocumentNode.AppendChild(node);

        }

        public void writeln(string text)
        {

            var node = new HtmlNode(HtmlNodeType.Text, document, -1);

            string innerhtml = "";

            for (int i = 0; i < text.Length; i++)
            {
                innerhtml += (text[i] + "\r\n");
            }

            node.InnerHtml = innerhtml;

            document.DocumentNode.AppendChild(node);

        }
    }

    public class History
    {
        public int length { get { return 0; } }

        public void back() { }

        public void forward() { }

        public void go(string uri) { }

        public void go(int number) { }
    }

    public class Location
    {
        public string hash { set; get; }
        public string host { get; set; }
        public string hostname { get; set; }
        public string href { set; get; }
        public string pathname { get; set; }
        public int port { get; set; }
        public string protocol { set; get; }
        public string search { set; get; }

        public void assign(string uri)
        {

        }

        public void reload(bool force)
        {

        }

        public void replace(string uri)
        {

        }
    }

    public class Navigator
    {
        //属性

        public string appCodeName { get { return "Browser"; } }
        public string appMinorVersion { get { return "1.0.0.0"; } }
        public string appName { get { return "Kugar.Core Headless Browser"; } }
        public string appVersion { get { return "1.0.0.0"; } }
        public string browserLanguage { get { return "zh-cn"; } }
        public bool cookieEnabled { get { return true; } }
        public string cpuClass { get { return "1"; } }
        public bool onLine { get { return true; } }
        public string platform { get { return "Win32"; } }
        public string systemLanguage { get { return "zh-cn"; } }
        public string userAgent { get; internal set; }
        public string userLanguage { get { return "zh-cn"; } }
        public HtmlNode[] plugins { get { return null; } }


        //方法

        public bool javaEnabled()
        {
            return false;
        }

        public bool taintEnabled()
        {
            return false;
        }


    }

    public class Screen
    {

    }
}
